const app = getApp();
import {
  AndTodayToLast,
  selcetPicture,
  isFrozen,
  wxscan,
  findPscode,
} from "../../server/api";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    obj: {},
    msg: {},
    user: {},
    msg: [],
    ordersid: "",
    show: false,
    hxvalue: "",
    beforeClose: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({ obj: {}, msg: [] });
    this.getTabBar().toggleMenu(2);
    this.getTabBar().init();
    this.getdata();
  },
  //获取数据
  async getdata() {
    const res = await AndTodayToLast();
    if (res.code == 200) {
      this.setData({ obj: res.result });
    }
    const data = await selcetPicture();
    if (data.code == 200) {
      this.setData({ user: data.result });
    }
  },
  //输入核销
  verification() {
    this.setData({ show: true });
  },
  //输入核销确认
  async getUserInfo() {
    this.setData({
      beforeClose: (action) =>
        new Promise(async (resolve) => {
          console.log(action, "aciton");
          setTimeout(async() => {
            if (action === "confirm") {
              if (this.data.hxvalue != "") {
                setTimeout(() => {
                  resolve(false);
                }, 500);
                const val = await isFrozen();
                if (val.result == "false") {
                  const params = {
                    psCode: this.data.hxvalue,
                  };
                  const data = await findPscode(params);
                  if (data.code == 200) {
                    const obj = JSON.stringify(data.result);
                    wx.navigateTo({
                      url: `/subPackagesB/pages/vercode/vercode?data=${obj}&hxvalue=${this.data.hxvalue}`,
                    });
                    this.setData({ show: false, hxvalue: "" });
                  }
             
                  
                } else {
                  wx.showToast({
                    title: "请输入核销码",
                    icon: "none",
                  });
                }
              } else {
                wx.showToast({
                  title: "请输入核销码",
                  icon: "none",
                });
              }
  
              resolve(false);
            } else if (action === "cancel") {
              this.setData({ hxvalue: "" });
              // 取消按钮
              resolve(true);
            }
          }, 100);
        
        }),
    });
  },
  //扫码核销
  scan() {
    wx.scanCode({
      success: async (res) => {
        //判断商家账号是否已被冻结
        const val = await isFrozen();
        if (val.result == "false") {
          this.setData({ ordersid: res.result });
          const params = {
            psCode: res.result,
          };
          const data = await findPscode(params);
          if (data.code == 200) {
            const obj = JSON.stringify(data.result);
            wx.navigateTo({
              url: `/subPackagesB/pages/scancode/scancode?data=${obj}&ordersid=${res.result}`,
            });
          }
        } else {
          wx.showToast({
            title: "您的账号已冻结,请联系管理员!",
            icon: "none",
          });
        }
      },
      fail: (err) => {
        console.error(err);
      },
    });
  },
  //核销记录
  record() {
    wx.setStorageSync("count", 8);
    wx.reLaunch({
      url: `/pages/verification/verification?type=7`,
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
